﻿#region Libraries

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;

#endregion

namespace XYZ_Inventory
{
    public partial class BusinessExpansionReports : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                XYZDBContext dbContext = new XYZDBContext();
                ddlYear.DataSource = dbContext.FinishedProductSales.GroupBy(x => x.DateAndTime.Year).Select(y => y.Key);
                ddlYear.DataBind();
                LoadSupplierLocation();
                LoadSalesGrowth();
            }
            Chart2.ChartAreas[0].AxisY.Title = "Sales";
            Chart2.ChartAreas[0].AxisX.Title = "Month";
            Chart2.ChartAreas[0].AxisX.Interval = 1;
        }

        private void LoadSupplierLocation()
        {
            Chart1.Series["Series"].ChartType =
                (SeriesChartType) Enum.Parse(typeof (SeriesChartType), "17");

            Series series1 = Chart1.Series["Series"];
            Chart1.Series["Series"]["PieLabelStyle"] = "Disabled";

            XYZDBContext dbContext = new XYZDBContext();

            List<SupplierLocations> results =
                dbContext.Suppliers1.GroupBy(x => x.District)
                    .Select(
                        y =>
                            new SupplierLocations
                            {
                                Location = y.FirstOrDefault().District,
                                Count = y.Count()
                            }).ToList();
            foreach (SupplierLocations r in results)
            {
                string districtName = dbContext.Districts.Single(x => x.Id == r.Location).DistrictName;
                series1.Points.AddXY(districtName, r.Count);
            }
        }

        private void LoadSalesGrowth()
        {
            XYZDBContext dbContext = new XYZDBContext();

            int year = Convert.ToInt32(ddlYear.SelectedValue);

            List<FinishedProductInventory> products = dbContext.FinishedProductInventories.ToList();
            foreach (FinishedProductInventory p in products)
            {
                Series series = new Series(p.Name)
                {
                    BorderWidth = 2,
                    ChartType = (SeriesChartType) Enum.Parse(typeof (SeriesChartType), "3")
                };
                Chart2.Series.Add(series);
            }
            for (int a = 1; a <= 12; a++)
            {
                List<Statistics> results =
                    dbContext.FinishedProductSales.Where(
                        x => x.DateAndTime.Year.Equals(year) && x.DateAndTime.Month.Equals(a)).GroupBy(x => x.ProductID)
                        .Select(
                            z =>
                                new Statistics
                                {
                                    Id = z.FirstOrDefault().ProductID,
                                    Count = z.Sum(l => l.Quantity)
                                }).ToList();

                string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(a);
                foreach (FinishedProductInventory p in products)
                {
                    Series series = Chart2.Series.Single(y => y.Name == p.Name);
                    try
                    {
                        Statistics s = results.Single(x => x.Id == p.ProductID);
                        series.Points.AddXY(monthName, s.Count);
                    }
                    catch
                    {
                        series.Points.AddXY(monthName, 0);
                    }
                }
            }
        }

        protected void ddlYear_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadSupplierLocation();
            LoadSalesGrowth();
        }
    }

    public class SupplierLocations
    {
        public int Location { get; set; }
        public int Count { get; set; }
    }
}